﻿using MyabpDDD.Demo.Application.yingfu.Dto;
using MyCompany.MyProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace MyabpDDD.Demo.Application.yingfu
{
    public class YingfuServicce : ApplicationService, IYingfuServicce
    {
        private readonly IRepository<Chargeablemanagement> _repository;
        private readonly IRepository<JieSuanTable> _JieSuanTable;
        public YingfuServicce(IRepository<Chargeablemanagement> students, IRepository<JieSuanTable> jieSuanTables)
        {
            this._repository = students;
            this._JieSuanTable = jieSuanTables;
        }
        public async Task Delete(int id)
        {
            var list = _repository.Where(r => r.Id == id).FirstOrDefault();

            await _repository.DeleteAsync(list);
        }

        public dynamic Get(int id)
        {
            var list = _repository.Where(r => r.Id == id).ToList();
            var q2 = _JieSuanTable.AsQueryable();
            var query = from a in list
                        join a3 in q2 on a.FangShi equals a3.Id
                        select new YinfuDto()
                        {
                            Id = a.Id,
                            NumBer = a.NumBer,
                            Danwei = a.Danwei,
                            FangShi = a.FangShi,
                            DunWei = a.Danwei,
                            Price = a.Price,
                            Sum = a.Sum,
                            YeWu = a.YeWu,
                            Ren = a.Ren,
                            CreateDate = a.CreateDate,
                            BeiZhu = a.BeiZhu,
                            Sistrte = a.Sistrte,
                            HeDui = a.HeDui,
                            HeDuiDate = a.HeDuiDate,
                            Name = a3.Name,
                        };
            var yuhe = query.ToList();
            var yuhd = yuhe[0];
            return yuhd;
        }

        public async Task<Chargeablemanagement> Inser(YinfuDto dto)
        {
            Chargeablemanagement www = ObjectMapper.Map<YinfuDto, Chargeablemanagement>(dto);
            return await _repository.InsertAsync(www);
        }

        public bool SelectDel(string ids)
        {
            string[] int_ids = ids.Split(',');
            var q = from id in int_ids
                    select Convert.ToInt32(id);
            var q_list = q.ToList();
            var query = _repository.AsQueryable();
            query = query.Where(d => q_list.Contains
              (d.Id));
            foreach (var model in query)
            {
                _repository.DeleteAsync(model);
            }
            return true;
        }

        public async Task<bool> update(YinfuDto dto)
        {
            Chargeablemanagement www = ObjectMapper.Map<YinfuDto, Chargeablemanagement>(dto);
            var db = _repository.GetDbSet<Chargeablemanagement>();
            db.Update(www);
            return true;
        }
    }
}
